package com.core.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.core.exception.CrashHandler;
import com.core.listener.I_Fragment;
import com.core.type.State;
import com.core.util.L;
import com.jxzyk.R;
import com.lidroid.xutils.ViewUtils;

/**
 * 
 * @Description: TODO(所有Fragment的父类)
 * 
 * @author gufei 562401002@qq.com
 * 
 * @date 2015年3月17日 上午10:00:44
 */
public abstract class BaseFragment extends Fragment implements I_Fragment {

	/************** Activity的状态 **************/
	public State fragmentState = State.DESTROY;

	private static Context mContext = null;

	public static Context getContext() {
		return mContext;
	}

	@Override
	public void showActivity(Class<?> cls) {
		mContext.startActivity(new Intent(getActivity(), cls));
	}

	@Override
	public void showActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent(getActivity(), cls);
		intent.putExtras(bundle);
		mContext.startActivity(intent);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		L.state(this.getClass(), "onCreate");
		mContext = getActivity();
		CrashHandler.instance(mContext);
		initData(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		L.state(this.getClass(), "onCreateView");
		View view = inflater.inflate(initUiRes(), null);
		ViewUtils.inject(this, view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		L.state(this.getClass(), "onActivityCreated");
		initUi(getView());
	}

	@Override
	public void onResume() {
		super.onResume();
		L.state(this.getClass(), "onResume");
		fragmentState = State.RESUME;
	}

	@Override
	public void onStart() {
		super.onStart();
		L.state(this.getClass(), "onStart");
	}

	@Override
	public void onPause() {
		super.onPause();
		L.state(this.getClass(), "onPause");
		fragmentState = State.PAUSE;
	}

	@Override
	public void onStop() {
		super.onStop();
		L.state(this.getClass(), "onStop");
		fragmentState = State.STOP;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		L.state(this.getClass(), "onDestroy");
		fragmentState = State.DESTROY;
	}
}
